using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLKS.HeThongBUS;

namespace QLKS
{
    public partial class ThongkePH : DevComponents.DotNetBar.Office2007Form
    {
        public ThongkePH()
        {
            InitializeComponent();
        }

        private void ckthang_CheckedChanged(object sender, EventArgs e)
        {
            if (ckthang.Checked == true)
            {
                cbbthang.Enabled = true;
            }
            else if (ckthang.Checked == false)
            {
                cbbthang.Enabled = false;
            }
        }

        
        private void cknam_CheckedChanged_1(object sender, EventArgs e)
        {
            if (cknam.Checked == true)
            {
                cbbnam.Enabled = true;
            }
            else if (cknam.Checked == false)
            {
                cbbnam.Enabled = false;
            }
        }
        string dk1;
        string dk2;
        string ngaythang;

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (ckthang.Checked == true)
            {
                dk1 = " And Month(CTThuePhong.NgayTraPhong) = '" + cbbthang.SelectedItem.ToString()+ "'";
                ngaythang = "Tháng " + cbbthang.SelectedItem.ToString();

            }
            if (ckthang.Checked == false)
            {
                dk1 = "";
            }
            if (cknam.Checked == true)
            {
                dk2 = " And Year(CTThuePhong.NgayTraPhong) = '" + cbbnam.SelectedItem.ToString() + "'";
                ngaythang += "  Năm " + cbbnam.SelectedItem.ToString();
            }
            if (cknam.Checked == false)
            {
                dk2 = "";
                ngaythang += "  Năm " + DateTime.Now.Year.ToString();
            }
            

            Thongke ds = new Thongke();
            DataTable t = ds.Tables.Add("Items");
            t.Columns.Add("maphong", Type.GetType("System.String"));
            t.Columns.Add("SoNgay", Type.GetType("System.Int32"));
            t.Columns.Add("tyle", Type.GetType("System.Double"));
            t.Columns.Add("ngaythang", Type.GetType("System.String"));
            DataTable dsp = TK_BUS.DSPhong();
            int dong = dsp.Rows.Count;
            DataRow r;
            int i = 0;
            double tongngay =0 ;
            double songay =0;
            // Tong ngay thue
            for (i = 0; i < dong; i++)
            {
                DataRow dr = dsp.Rows[i];

                DataTable dt = TK_BUS.ThongKe(dr["MaPhong"].ToString(),dk1,dk2);
                int dem = dt.Rows.Count;
                for (int j = 0; j < dem; j++)
                {
                    DataRow dr1 = dt.Rows[j];
                    DateTime d1 = DateTime.Parse(dr1["NgayThuePhong"].ToString());
                    DateTime d2 = DateTime.Parse(dr1["NgayTraPhong"].ToString());
                    TimeSpan temp = d2 - d1;
                    double ngay = temp.TotalDays;
                   // txtnam.Text += ngay.ToString();
                    
                    songay = songay + ngay;
                    //if (d2 != null)
                    //{
                    //    TimeSpan temp = d2 - d1;
                    //    double ngay = temp.TotalDays;
                    //    songay = songay + ngay;

                    //}
                    //else if (d2 == null)
                    //{
                    //    songay = 0;
                    //}
                    
                    
                }
               // txtnam.Text += "/";
                
                
                tongngay = tongngay + songay;

                songay = 0;
            }
           
            // Tao bảng
            for (i = 0; i < dong; i++)
            {
                DataRow dr = dsp.Rows[i];
                r = t.NewRow();
                r["maphong"] = dr["MaPhong"].ToString();
                DataTable dt = TK_BUS.ThongKe(dr["MaPhong"].ToString(),dk1,dk2);

                int dem = dt.Rows.Count;
                
                for (int j = 0; j < dem; j++)
                {
                    DataRow dr1 = dt.Rows[j];
                    DateTime d1 = DateTime.Parse(dr1["NgayThuePhong"].ToString());
                    DateTime d2 = DateTime.Parse(dr1["NgayTraPhong"].ToString());
                    TimeSpan temp = d2 - d1;
                    double ngay = temp.TotalDays;
                    songay = songay + ngay;
                    
                    //if (d2 != null)
                    //{
                    //    TimeSpan temp = d2 - d1;
                    //    double ngay = temp.TotalDays;
                    //    songay = songay + ngay;
                    //}
                    //else if (d2 == null)
                    //{
                    //    songay = 0;
                    //}
                    
                }
                
               
                r["Songay"] = songay;
                
                if (songay == 0) 
                {
                    double s = 0;
                    r["Tyle"] = s;
                }
                else
                {
                    double s = (songay / tongngay) * 100;
                    r["Tyle"] = s;
                }

                r["ngaythang"] = ngaythang;
                t.Rows.Add(r);
                songay = 0;
            }

            ThongkePhong objRpt = new ThongkePhong();
            objRpt.SetDataSource(ds.Tables[1]);
            
            // objRpt.Parameter_thang = thang;
            this.crystalReportViewer1.ReportSource = objRpt;
            this.crystalReportViewer1.Refresh(); 
            

        }

        private void ThongkePH_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 12; i++)
            {
                cbbthang.Items.Add(i);
            }
            int namht = Int32.Parse(DateTime.Now.Year.ToString());
            int nambd = 2009;
            for (int i = nambd; i <= namht; i++)
            {
                cbbnam.Items.Add(i);
            }
        }
    }
}